package com.jh.user.repository.freeQuick;


import com.jh.user.pojo.freeQuick.MerchantBankCard;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface MerchantBankCardRepository extends JpaRepository<MerchantBankCard, Long>,
        JpaSpecificationExecutor<MerchantBankCard> {


    MerchantBankCard findByMerchantIdAndCardNo(String merchantId, String cardNo);

    MerchantBankCard findByMerchantId(String merchantId);

    @Modifying
    int deleteByMerchantIdAndCardNo(String merchantId, String cardNo);


    @Query("from  MerchantBankCard mbc where mbc.merchantId=:merchantId and mbc.idDef=:idDef  and mbc.state='0'")
    MerchantBankCard findByMerchantIdAndIdDef(@Param("merchantId") String merchantId, @Param("idDef") String idDef);

    @Query("from  MerchantBankCard mbc where mbc.merchantId=:merchantId and mbc.cardNo=:bankCode and mbc.state='0' and mbc.merchantName like %:merchantName% ")
    Page<MerchantBankCard> queryMerchantBankCardByMerchantIdAndBankCodeAndMerchantName(@Param("merchantId") String merchantId, @Param("bankCode") String bankCode, @Param("merchantName") String merchantName, Pageable pageable);

    @Query("from  MerchantBankCard mbc where  mbc.cardNo=:bankCode and mbc.merchantName like %:merchantName%  and mbc.state='0'")
    Page<MerchantBankCard> findByBankCodeAndMerchantName(@Param("bankCode") String bankCode, @Param("merchantName") String merchantName, Pageable pageable);

    @Query("from  MerchantBankCard mbc where mbc.merchantId=:merchantId and  mbc.merchantName like %:merchantName%  and mbc.state='0'")
    Page<MerchantBankCard> findByMerchantIdAndMerchantName(@Param("merchantId") String merchantId, @Param("merchantName") String merchantName, Pageable pageable);

    @Query("from  MerchantBankCard mbc where mbc.merchantId=:merchantId and mbc.cardNo=:bankCode and mbc.state='0'")
    Page<MerchantBankCard> findByMerchantIdAndBankCode(@Param("merchantId") String merchantId, @Param("bankCode") String bankCode, Pageable pageable);

    @Query("from  MerchantBankCard mbc where mbc.merchantId=:merchantId and mbc.state='0'")
    Page<MerchantBankCard> findByMerchantId(@Param("merchantId") String merchantId, Pageable pageable);

    @Query("from  MerchantBankCard mbc where mbc.cardNo=:cardNo and mbc.state='0'")
    Page<MerchantBankCard> findByCardNo(@Param("cardNo") String cardNo, Pageable pageable);

    @Query("from  MerchantBankCard mbc where  mbc.merchantName like %:merchantName%  and mbc.state='0'")
    Page<MerchantBankCard> findByMerchantNameLike(@Param("merchantName") String merchantName, Pageable pageable);

    @Modifying
    @Query("update MerchantBankCard set idDef=:idDef where merchantId=:merchantId")
    void updateIdDefByMerchantId(@Param("merchantId") String merchantId, @Param("idDef") String idDef);

    @Modifying
    @Query("update MerchantBankCard set idDef=0 where merchantId=:merchantId and idDef=1")
    void updateNoDefaultCard(@Param("merchantId") String merchantId);

    @Query("from MerchantBankCard mbc where mbc.merchantId=:merchantId and mbc.cardType=:cardType and mbc.state=0")
    List<MerchantBankCard> findByMerchantIdAndCardType(@Param("merchantId") String merchantId, @Param("cardType") String cardType);

}
